﻿using System;
using System.Windows.Forms;
using WinFormsGraphicsDevice;
using MegavaniaX;
using MegavaniaX.World;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using ButtonState = Microsoft.Xna.Framework.Input.ButtonState;
namespace MegavaniaXLevelEditor
{
    public partial class TileListerControl : GraphicsDeviceControl
    {
        private SpriteBatch sb;
        private float Zoom = 3.0f;

        public string CurrentTileSubset = string.Empty;

        private ContentBuilder contentBuilder;
        private ContentManager contentManager;
        private TextureRepo myrepo;
        private KeyboardState oldKeyState;
        private MouseState oldMouseState;

        public MegavaniaXLevelEditorMain editor;

        protected override void Initialize()
        {
            myrepo = new TextureRepo();
            Application.Idle += delegate { Invalidate(); };
            contentBuilder = new ContentBuilder();
            contentManager = new ContentManager(this.Services, contentBuilder.OutputDirectory);
            sb = new SpriteBatch(GraphicsDevice);
            string be = myrepo.AddTilesToContent(contentBuilder, contentManager);
            if (!string.IsNullOrEmpty(be))
            {
                MessageBox.Show(be);
            }

            CurrentTileSubset = @"levels\magnet\";

            oldKeyState = Keyboard.GetState();
            oldMouseState = Mouse.GetState();
        }

        private void UpdateThings()
        {
            Mouse.WindowHandle = this.Handle;
            var mouseState = Mouse.GetState();

            if (mouseState.LeftButton == ButtonState.Pressed && !(oldMouseState.LeftButton == ButtonState.Pressed))
            {
                editor.currentlySelectedTileName = GetHoveredTileName(mouseState.X, mouseState.Y);
            }

            oldMouseState = mouseState;
        }

        private string GetHoveredTileName(int mouseX, int mouseY)
        {
            //Console.WriteLine(mouseX.ToString() + " " + mouseY.ToString());
            // (0px-18px) * zoom
            
            return "testing";
        }


        protected override void Draw()
        {
            UpdateThings();

            GraphicsDevice.Clear(Color.Black);            
            if (string.IsNullOrEmpty(CurrentTileSubset))
                return;


            sb.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
            GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp;
            var currentTiles = myrepo.GetTextureList(CurrentTileSubset);
            float x = 0;
            float y = 0;
            foreach (var tile in currentTiles)
            {
                if (x + 18.0f * Zoom > this.Width)
                {
                    x = 0.0f;
                    y += 18.0f * Zoom;
                }

                sb.Draw(tile, new Vector2(x, y), null, Color.White, 0.0f, Vector2.Zero, Zoom, SpriteEffects.None, 0);

                x += 18.0f * Zoom;
            }

            sb.End();
        }

    }
}
